package com.lightjet.macross.base.upms.controller;

import com.lightjet.macross.base.upms.domain.AppClient;
import com.lightjet.macross.base.upms.service.AppClientService;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/appclient")
@Scope("request")
@Log
@RefreshScope
@Api(tags="通用系统权限管理-接入客户端相关接口集")
public class AppClientController {

    @Resource
    private AppClientService appClientService;

    @RequestMapping(value = "/query", method = {RequestMethod.GET})
    @ApiOperation(value = "客户端查询接口", notes = "根据关键字查询客户端列表")
    public ResponseDTO<List<AppClient>> GetDeptsByKeywords(String keywords) {
        return RespUtil.CreateOK(appClientService.FindByKeywords("%"+keywords+"%"));
    }

    @PostMapping(value="",consumes = "application/json")
    @ApiOperation(value = "客户端信息创建接口", notes = "根据客户端信息实体类Json创建客户端")
    public ResponseDTO<AppClient> CreateAppClient(
            @ApiParam(name = "appclient", value = "客户端实体类Json", required = true, type="body") @RequestBody AppClient appclient)
    {
        if(StringUtil.isNullOrEmpty(appclient.getSecret()))
        {
            appclient.setSecret(UUID.randomUUID().toString());
        }
        if(StringUtil.isNullOrEmpty(appclient.getClientId()))
        {
            appclient.setClientId(UUID.randomUUID().toString());
        }
        return RespUtil.CreateOK(appClientService.CreateAndFlush(appclient));
    }

    @PutMapping(value="",consumes = "application/json")
    @ApiOperation(value = "客户端信息更新接口", notes = "根据客户端信息实体类Json更新客户端")
    public ResponseDTO<AppClient> UpdateAppClient(
            @ApiParam(name = "appclient", value = "客户端实体类Json", required = true, type="body") @RequestBody AppClient appclient)
    {
        AppClient origin = appClientService.FindDistinctById(appclient.getId());
        if(origin!=null)
        {
            origin.setAccesstokenspan(appclient.getAccesstokenspan());
            origin.setRefreshtokenspan(appclient.getRefreshtokenspan());
            origin.setClientId(appclient.getClientId());
            origin.setClientSecret(appclient.getClientSecret());
            origin.setClientname(appclient.getClientname());
            origin.setIsenable(appclient.getIsenable());
            origin.setEntryuri(appclient.getEntryuri());
            origin.setGranttypes(appclient.getGranttypes());
            origin.setRegisteruri(appclient.getRegisteruri());
            origin.setGrantedresources(appclient.getGrantedresources());
            origin.setGrantedauthorities(appclient.getGrantedauthorities());
            origin.setGrantedscope(appclient.getGrantedscope());
            origin.setDescription(appclient.getDescription());
        }
        else
        {
            return RespUtil.CreateError("客户端【"+appclient.getId()+"】不存在");
        }
        return RespUtil.CreateOK(appClientService.UpdateSingle(origin));
    }

    @DeleteMapping(value="")
    @ApiOperation(value = "客户端信息删除接口", notes = "根据客户端ID（Integer）删除客户端")
    public ResponseDTO<String> DeleteAppClient(@ApiParam(name="id",value = "客户端ID",required = true, type="query") Integer id)
    {
        try
        {
            appClientService.DeleteOne(new AppClient(id));
            return RespUtil.CreateOK();
        }
        catch (Exception e)
        {
            return RespUtil.CreateError("删除客户端失败："+e.getMessage());
        }
    }

    @DeleteMapping(value="/batch")
    @ApiOperation(value = "客户端信息批量删除接口", notes = "根据客户端IDI组ListJson批量删除")
    public ResponseDTO<String> DeleteAppClient(@ApiParam(name = "ids",value = " List Json形式",type="body") @RequestBody List<Integer> ids)
    {
        StringBuilder sb = new StringBuilder();
        for(Integer id : ids)
        {
            try
            {
                appClientService.DeleteSingle(new AppClient(id));
            }
            catch (Exception e)
            {
                sb.append(MessageFormat.format("删除客户端信息【{0}】删除失败：{1};",id,e.getMessage()));
            }
        }
        if(sb.length()>0)
        {
            return RespUtil.CreateError(sb.toString());
        }
        else
        {
            return RespUtil.CreateOK();
        }
    }
}
